package com.senseuni.da.ui

import android.app.Dialog
import android.os.Bundle
import android.provider.Settings
import android.provider.Settings.System.SCREEN_BRIGHTNESS
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.SeekBar
import androidx.fragment.app.Fragment
import com.senseuni.common.dialog.OnDialogResultListener
import com.senseuni.da.R
import com.senseuni.da.databinding.FragmentDisplayBinding
import com.senseuni.da.ui.dialog.SleepSettingDialog
import com.senseuni.da.utils.SpUtil


class DisplayFragment : Fragment() {

    companion object {
        fun newInstance() = DisplayFragment()
    }

    private lateinit var binding: FragmentDisplayBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDisplayBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.seekbarBright.max = 255

        binding.sivSleepMode.setOnClickListener {
            val dialog = SleepSettingDialog(requireContext())
            dialog.setOnResultListener { _, _ -> updateSleepMode() }
            dialog.create().show()
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        //获取当前亮度
        val contentResolver = requireContext().contentResolver
        val brightness = Settings.System.getInt(contentResolver, SCREEN_BRIGHTNESS, 0)
        binding.seekbarBright.progress = brightness

        binding.seekbarBright.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                setBrightness(progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        updateSleepMode()
    }

    private fun updateSleepMode() {
        val isSleepEnabled = SpUtil.isSleepEnabled()
        if (!isSleepEnabled) {
            binding.sivSleepMode.setSummary(getString(R.string.sleep_mode_off_tip))
        } else {
            val sleepTime = SpUtil.getSleepTime()
            val activeTime = SpUtil.getActiveTime()
            binding.sivSleepMode.setSummary(getString(R.string.sleep_mode_on_tip, sleepTime, activeTime))
        }
    }

    private fun setBrightness(value: Int) {
        if (!Settings.System.canWrite(requireContext())) {
            return
        }
        Settings.System.putInt(requireContext().contentResolver, SCREEN_BRIGHTNESS, value)
        Settings.System.putInt(requireContext().contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)

        val layoutParams: WindowManager.LayoutParams = activity?.window?.attributes!!
        layoutParams.screenBrightness = value / 255f // 将整数值转换为0-1之间的浮点数
        activity?.window?.attributes = layoutParams
    }

}